package cn.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ConfigOption;
import cn.common.ExceResult;
import cn.common.ExcelHandler;
import cn.common.GridCommon;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.common.utils.ChineseCharToPingYinUtils;
import cn.kpi.entity.attendance.AttSummaryEntity;
import cn.kpi.entity.pingyou.ByUserEntity;
import cn.kpi.entity.pingyou.UserNdkhEntity;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.service.pingyou.INdkhpsService;
import cn.kpi.service.sys.IOrgService;

@Controller
@RequestMapping("ndkhps")
public class NdkhpsController extends BaseController {
	
	@Autowired
	private INdkhpsService iNdkhpsService;
	
	@Autowired
	private IOrgService iOrgService;
	
	@RequestMapping(value = "/ndkhtb_list")
	public String NdkhtbList(Model model) {
		try{
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			year = year -1;
			model.addAttribute("year", year);
		}
		catch(Exception ex){
			
		}
		return "pingyou/ndkhtb_list";
	}
	
	@RequestMapping(value = "/ndkhhz_list")
	public String NdkhhzList(Model model) {
		try{
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			year = year -1;
			model.addAttribute("year", year);
			
		}
		catch(Exception ex){
			
		}
		return "pingyou/ndkhhz_list";
	}
	
	
	@RequestMapping(value = "/ndkhtb_tz")
	public String NdkhtbTz(Model model,Long ndkhpsid) {
		try{
			UserNdkhEntity entity=iNdkhpsService.GetNdkhpsEntityByUserid(ndkhpsid);
			model.addAttribute("entity", entity);
			
		}
		catch(Exception ex){
			
		}
		return "pingyou/ndkhtb_tz";
	}
	
	@RequestMapping(value = "/ndkh_status")
	public String NdkhStatus(Model model,Long orgstatustjid,int status,int orgid,int year) {
		try{
			
			model.addAttribute("orgstatustjid", orgstatustjid);
			model.addAttribute("status", status);
			model.addAttribute("orgid", orgid);
			model.addAttribute("year", year);
			
		}
		catch(Exception ex){
			
		}
		return "pingyou/ndkh_status";
	}
	
	
	
	@ResponseBody
	@RequestMapping(value = "/get_list_org")
	public GridCommon<Map> GetListOrg(HttpServletRequest request) {
		GridCommon<Map> countlist = new GridCommon<Map>();
		try {
			int year = Integer.parseInt(request.getParameter("year"));
			String where=" AND orgid IN ("+this.GetOrgListByRole()+")";

			List<Map> list = iNdkhpsService.GetPjOrgSumList(where, year);
			
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(list);
			countlist.setStatus(0);
			countlist.setMsg("");
		} catch (Exception e) {
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(null);
			countlist.setStatus(1);
			countlist.setMsg("操作成功");
		}
		return countlist;
	}
	
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_ndkh_detail_list")
	public LayTable<UserNdkhEntity> GetNdkhDetailList(HttpServletRequest request) {

		List<UserNdkhEntity> lst = null;
		LayTable<UserNdkhEntity> layTable = new LayTable<UserNdkhEntity>();
		try {
			int year =Integer.parseInt(request.getParameter("year")) ;
			String realname = request.getParameter("realname");
			String szdw = request.getParameter("szdw");
			String khjg=request.getParameter("khjg");
			
			String where = "";
			
			
            
            if(szdw!=null&&szdw.length()>0&&!szdw.equals("0")){
            	OrgEntity orgentity=iOrgService.GetOrgById(Long.parseLong(szdw));
            	where+=" AND szdw IN (SELECT orgid FROM t_sys_org WHERE lft>="+orgentity.getLft()+" AND lft<="+orgentity.getRgt()+")";
            	
			}
            else{
            	 String strorgids=this.GetOrgListByRole();
 				if(strorgids!=null&&strorgids.length()>0){
 					where+= " AND szdw IN ("+strorgids+") ";
 				}
 				else{
 					where+= " AND userid="+this.GetLoginUserId();
 				}
            }
            
            if(khjg!=null&&khjg.length()>0){
            	where+=" AND ndkhpsjg='"+khjg+"'";
            }
            
            
            int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			int beginrow=(page-1)*limit;
			int endrow=limit;
            
			//String wherelimit=where+" ORDER BY C.orderindex LIMIT "+beginrow+","+endrow;
			
			lst = iNdkhpsService.GetNdkhpsListByWhere(where, realname, year,beginrow,endrow);
			for (int i = 0; i < lst.size(); i++) {
				int numr=(page-1)*limit+(i + 1);
				lst.get(i).setRowNum(numr);
			}
			int iCount = iNdkhpsService.GetNdkhpsCountByWhere(where,realname, year);
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/create_ndkh")
	public ExceResult CreateNdkh(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long orgid=Long.parseLong(request.getParameter("orgid"));
		 int year=Integer.parseInt(request.getParameter("year"));
		 Long byuserid=this.GetLoginUserId();
		 
		 ByUserEntity entity=new ByUserEntity();
		 entity.setByuserid(byuserid);
		 entity.setOrgid(orgid);
		 entity.setYear(year);
		 
		 int iResult=iNdkhpsService.InsertNdkhDetail(entity);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	
	@RequestMapping(value = "/update_ndkh_status")
	public String UpdateNdkhStatus(HttpServletRequest request,HttpServletResponse response){
		try{
			String ndkhpsjg=request.getParameter("ndkhpsjg");
			long ndkhpsid=Long.parseLong(request.getParameter("ndkhpsid"));
			String remark=request.getParameter("remark");
			
			int iResult=iNdkhpsService.UpdateNdkhDetail(ndkhpsid, ndkhpsjg, remark);
			
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			ex.printStackTrace();
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	
	@RequestMapping(value = "/update_ndkh_org_status")
	public String UpdateNdkhOrgStatus(HttpServletRequest request,HttpServletResponse response){
		try{
			long orgstatustjid=Long.parseLong(request.getParameter("orgstatustjid"));
			String remark=request.getParameter("remark");
			int status=Integer.parseInt(request.getParameter("status"));
			
			long orgid=Long.parseLong(request.getParameter("orgid"));
			int year=Integer.parseInt(request.getParameter("year"));
			
			long byuserid=this.GetLoginUserId();
			
			int iResult=iNdkhpsService.UpdateNdkhOrg(orgstatustjid, status, remark, byuserid,orgid,year);
			
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			ex.printStackTrace();
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/export_excel_all")
	public ExceResult ExportExcelAll(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
			int year =Integer.parseInt(request.getParameter("year")) ;
			String realname = request.getParameter("realname");
			String szdw = request.getParameter("szdw");
			String khjg=request.getParameter("khjg");
			
			String where = "";
			
			
            
            if(szdw!=null&&szdw.length()>0&&!szdw.equals("0")){
            	OrgEntity orgentity=iOrgService.GetOrgById(Long.parseLong(szdw));
            	where+=" AND szdw IN (SELECT orgid FROM t_sys_org WHERE lft>="+orgentity.getLft()+" AND lft<="+orgentity.getRgt()+")";
            	
			}
            else{
            	 String strorgids=this.GetOrgListByRole();
 				if(strorgids!=null&&strorgids.length()>0){
 					where+= " AND szdw IN ("+strorgids+") ";
 				}
 				else{
 					where+= " AND userid="+this.GetLoginUserId();
 				}
            }
            
            if(khjg!=null&&khjg.length()>0){
            	where+=" AND ndkhpsjg='"+khjg+"'";
            }
            
            
           
			int beginrow=0;
			int endrow=10000;
			
			List<UserNdkhEntity> lst = iNdkhpsService.GetNdkhpsListByWhere(where, realname, year,beginrow,endrow);
			for (int i = 0; i < lst.size(); i++) {
				int numr=(i + 1);
				lst.get(i).setRowNum(numr);
			}
			
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
            
            //String finalXlsxPath=ConfigOption.getInstance().getTemplateurl()+"/kqtemp.xlsx";
            //String filename=ConfigOption.getInstance().getTemplateurl()+"/考勤_"+sdf.format(new Date())+".xlsx";
            
            String finalXlsxPath=ConfigOption.getInstance().getTemplateurl()+"/ndkhps.xlsx";
            String filename="ndkhps_"+sdf.format(new java.util.Date())+".xlsx";
            Boolean olResult=ExcelHandler.writeExcelNdkhps(lst, finalXlsxPath, filename,response,year);
		    if(olResult){
				result.setCode(200);
				result.setResult(1);
				String urlen=ConfigOption.getInstance().getWeburl()+filename;
				result.setMsg(urlen);
			}
			else{
				result.setCode(300);
				result.setResult(0);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
}
